<?php
/** 
* 下载文件header函数 
* copyright by www.mimvp.com 
* 2015-05-10 
*/  
   
$res_filepath = "";  
if(isset($_GET["filepath"])) {  
    $res_filepath = $_GET["filepath"];  
}  
   
//     $filepath = "./lib/tmp_txt_result_file_20150508170116.txt";  
    $file_realpath = realpath($res_filepath);  
    $file_basename = basename($res_filepath);  
//     $file_filesize = filesize($res_filepath);  
    $file_fileinfo = pathinfo($res_filepath);  
   
    if (!file_exists($res_filepath)){  
        header("Content-type: text/html; charset=utf-8");  
        echo "<html>  
                <div style='margin-left: 20px'>  
                    <br>  
                    <font color='blue'>$file_basename</font> 是临时文件已过期，服务器不保存！  
                    <br><br>  
                    请提取最新代理： <a href='../fetch.php'>http://proxy.mimvp.com/api/fetch.php</a>  
                       
                    <!--  
                    <script>  
                    alert('" . $file_basename . "\\n是临时文件，服务器不保存! \\n\\n请重新提取最新代理');  
                     </script>  
                    -->  
                </div>  
              </html>";  
    } else {  
        $file_filesize = filesize($res_filepath);  
        $file = fopen($res_filepath, "r");  
        Header("Content-type: application/octet-stream");  
        Header("Accept-Ranges: bytes");  
        Header("Accept-Length: " . $file_filesize);  
        Header("Content-Disposition: attachment; filename=" . $file_basename);  
        echo fread($file, $file_filesize);  
        fclose($file);  
//         echo file_get_contents($filename);  
//         readfile($filename);  
    }  
   
    // 下载或取消后，删除临时文件  
    $del_result = @unlink($res_filepath);  
    if ($del_result == true) {  
        @unlink($res_filepath);  
    }  
?>